import { useState, useEffect } from 'react';

import store from '../redux/store';
import { getCinemaList } from '../redux/action/CinemaActionCreator';

const useCinemaList = (cityId) => {
  const [list, setList] = useState(store.getState().CinemaReducer.cinemaList);

  useEffect(() => {
    if (store.getState().CinemaReducer.cinemaList.length === 0) {
      store.dispatch(getCinemaList(cityId));
    } else {
      console.log('useCinema 缓存');
    }
    return () => {};
  }, [cityId]);

  useEffect(() => {
    // 订阅
    let unsubscribe = store.subscribe(() => {
      setList(store.getState().CinemaReducer.cinemaList);
    });
    return () => {
      // 取消订阅
      unsubscribe();
    };
  }, []);
  return {
    list,
  };
};

export { useCinemaList };
